package com.zzyl.nursing.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zzyl.nursing.dto.DeviceDelDto;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.dto.DevicePropertiesVo;
import com.zzyl.nursing.dto.PageDeviceDto;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备管理Controller
 *
 * @author ruoyi
 * @date 2025-08-26
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备管理的接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询所有产品列表
     */
    @ApiOperation("查询所有产品列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:allProduct')")
    @GetMapping("/allProduct")
    public AjaxResult allProduct() {
        List<ProductVo> list = deviceService.getProductList();
        return AjaxResult.success(list);
    }

    /**
     * 查询设备管理列表
     */
    @ApiOperation("查询设备管理列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 同步产品列表
     * @return
     */
    @PostMapping("/syncProductList")
    @PreAuthorize("@ss.hasPermi('nursing:device:syncProductList')")
    @ApiOperation(value = "从物联网平台同步产品列表")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 注册设备
     * @param deviceDto
     * @return
     */
    @PostMapping("/register")
    @PreAuthorize("@ss.hasPermi('nursing:device:register')")
    @ApiOperation(value = "注册设备")
    public AjaxResult registerDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.registerDevice(deviceDto);
        return AjaxResult.success();
    }

    /**
     * 设备详情
     * @param iotId
     * @return
     */
    @GetMapping("/{iotId}")
    @PreAuthorize("@ss.hasPermi('nursing:device:{iotId}')")
    @ApiOperation(value = "设备详情")
    public AjaxResult deviceDetail(@PathVariable String iotId) {
        DeviceDetailVo detailVo = deviceService.deviceDetail(iotId);
        return AjaxResult.success(detailVo);
    }

    /**
     * 设备详情
     * @param iotId
     * @return
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @PreAuthorize("@ss.hasPermi('nursing:device:queryServiceProperties:{iotId}')")
    @ApiOperation(value = "查询指定设备的物模型运行状态")
    public AjaxResult queryDeviceServiceProperties(@PathVariable String iotId) {
        List<DevicePropertiesVo> list = deviceService.queryDeviceServiceProperties(iotId);
        return AjaxResult.success(list);
    }

    /**
     * 删除设备
     * @return
     */
    @DeleteMapping("/{iotId}")
    @PreAuthorize("@ss.hasPermi('nursing:device')")
    @ApiOperation(value = "删除设备")
    public AjaxResult deleteDevice(@PathVariable String iotId) {
        deviceService.deleteDevice(iotId);
        return AjaxResult.success();
    }

}
